import { System } from "./System";
import { ComponentType } from "../component/Component";
import { Register } from "../register/Register";
/**
 * @class
 * @extends w.system.System
 * @memberof w.system
 */
class MeshSystem extends System {
  constructor(opts = {}) {
    super(opts);
  }
  run(event) {
    if (event.componentType !== ComponentType.Mesh) {
      return;
    }
    const meshRender = Register.instance.manager(ComponentType.MeshRender).get('x');
    let mesh = event.target;
    let trans = this.getTrans(mesh);
    if (!trans) {
      return;
    }
    let groups = mesh.getGroup();
    if (trans.hash === "trans") {//单实例
      let id = mesh.id;
      let hash = mesh.hash + "trans";
      let exitMesh = meshRender.meshes.get(id);
      if (exitMesh) {
        if (mesh.visibility) {
          groups = this.merge(exitMesh.groups, groups);
          if (exitMesh.hash == hash && exitMesh.groups.length == groups.length) {
            //更新
            meshRender.addValueChange(exitMesh.id);
          } else if (exitMesh.hash == hash) {
            //从老的groups移除
            meshRender.remove(exitMesh);
            //附加到新的groups上。
            exitMesh.groups = groups;
            meshRender.add(exitMesh);
          } else {
            meshRender.remove(exitMesh);
            //重新生成并添加
            let renderInfo = { id, hash, groups };
            meshRender.add(renderInfo);
          }
        } else {
          meshRender.remove(exitMesh);
        }

      } else {
        if (mesh.visibility) {
          let renderInfo = { id, hash, groups };
          meshRender.add(renderInfo);
        }

      }
    } else {//多实例
      let id = mesh.id;
      let hash = mesh.hash + "transinstance"+trans.instanceCount;
      let exitMesh = meshRender.meshes.get(id);
      if (exitMesh) {
        if (mesh.visibility) {
          groups = this.merge(exitMesh.groups, groups);
          if (exitMesh.hash == hash && exitMesh.groups.length == groups.length) {
            //更新
            meshRender.addValueChange(exitMesh.id);
          } else if (exitMesh.hash == hash) {
            //从老的groups移除
            meshRender.remove(exitMesh);
            //附加到新的groups上。
            exitMesh.groups = groups;
            meshRender.add(exitMesh);
          } else {
            meshRender.remove(exitMesh);
            //重新生成并添加
            let renderInfo = { id, hash, groups };
            meshRender.add(renderInfo);
          }
        } else {
          meshRender.remove(exitMesh);
        }
      } else {
        if (mesh.visibility) {
          let renderInfo = { id, hash, groups };
          meshRender.add(renderInfo);
        }

      }
    }


  }
  merge(o, t) {
    let all = [...o];
    for (let i = 0; i < t.length; i++) {
      if (all.indexOf(t[i]) === -1) {
        all.push(t[i]);
      }
    }
    return all;
  }
  getTrans(mesh) {
    let trans;
    const eid = [...mesh.eids][0];
    if (eid) {
      const entity = Register.instance.entityManager.get(eid);
      if (entity) {
        trans = entity.getComponent(ComponentType.Trans);
      }
    }
    return trans;
  }
}
export { MeshSystem }